package com.spring.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.spring.adapter.LocationAdapter;
import com.spring.gowhere.R;
import com.spring.httputils.RequestParams;
import com.spring.httputils.RestClient;
import com.spring.httputils.TextHttpResponseHandler;
import com.spring.tool.App;
import com.spring.util.Constant;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 修改个人资料
 * @author Administrator
 *
 */
public class ChangeEmplInfoActivity extends BaseActivity{
	RestClient restClient = null;
	private RelativeLayout location;
	private TextView location_text;
	private String location_textStr;
	private Button btnlogin;
	String usernames = null;
	String logintypes = null;
	public EditText edusername, eduserpass, edIMs, edEmail, edAddress,edLinkMan,edTel;
	public String logintype, username, StredIMs,StredEmail,StredAddress,StredLinkMan,StredTel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.changeemplinfo);
		SharedPreferences userInfo = getSharedPreferences("user_info", 0);
		usernames = userInfo.getString("username", "");
		logintypes = userInfo.getString("logintype", "");
		ImageButton imageButton = (ImageButton) findViewById(R.id.title_left_ib);
		imageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		TextView textView = (TextView) findViewById(R.id.title_tv);
		textView.setText("修改个人资料");
		location = (RelativeLayout) findViewById(R.id.reservation_location);
		location_text = (TextView) findViewById(R.id.reservation_location_text);
		if(logintypes!=null){
			if(logintypes.equals("1")){
				location_textStr="身份证号";
			}else if(logintypes.equals("2")){
				location_textStr="学号";
			}else if(logintypes.equals("3")){
				location_textStr="邮箱地址";
			}//
		}
		location_text.setText(location_textStr);
		edusername = (EditText) findViewById(R.id.edusername);
		eduserpass = (EditText) findViewById(R.id.eduserpass);
		edIMs = (EditText) findViewById(R.id.edIMs);
		edEmail = (EditText) findViewById(R.id.edEmail);
		edAddress =(EditText)findViewById(R.id.edAddress);
		edLinkMan =(EditText)findViewById(R.id.edLinkMan);
		edTel =(EditText)findViewById(R.id.edTel);
		btnlogin = (Button) findViewById(R.id.btnlogin);
		edusername.setText(usernames);
		edusername.setFocusable(false);
		location.setFocusable(false);
		boolean islianwang = new Constant().isConnect(ChangeEmplInfoActivity.this);
			btnlogin.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if(!new Constant().isConnect(ChangeEmplInfoActivity.this)){
						return;
					}
					username = edusername.getText().toString();
					
					StredIMs = edIMs.getText().toString();
					
					StredEmail = edEmail.getText().toString();
					
					StredAddress = edAddress.getText().toString();
					StredLinkMan = edLinkMan.getText().toString();
					StredTel = edTel.getText().toString();
					if(location_textStr==null||"".equals(location_textStr)){
						Toast.makeText(ChangeEmplInfoActivity.this, "请选择你的登录方式", Toast.LENGTH_SHORT).show();
					}else if(StredIMs==null||"".equals(StredIMs)){
						Toast.makeText(ChangeEmplInfoActivity.this, "请输入你的即时通讯",  Toast.LENGTH_SHORT).show();
					}
					else if(StredEmail==null||"".equals(StredEmail)){
						Toast.makeText(ChangeEmplInfoActivity.this, "请输入你的邮箱",  Toast.LENGTH_SHORT).show();
					}else if(StredAddress==null||"".equals(StredAddress)){
						Toast.makeText(ChangeEmplInfoActivity.this, "请输入你的地址",  Toast.LENGTH_SHORT).show();
					}else if(StredLinkMan==null||"".equals(StredLinkMan)){
						Toast.makeText(ChangeEmplInfoActivity.this, "请输入你的联系人",  Toast.LENGTH_SHORT).show();
					}
					else if(StredTel==null||"".equals(StredTel)){
						Toast.makeText(ChangeEmplInfoActivity.this, "请输入你的电话",  Toast.LENGTH_SHORT).show();
					}else{						
						restClient = new RestClient(ChangeEmplInfoActivity.this);
						SendHttpDate(logintype, username, StredIMs, StredEmail,StredAddress,StredLinkMan,StredTel);
					}
				}
			});
		}


	/**
	 * 体检地点选择对话框
	 * 
	 * @author Administrator
	 * 
	 */
	private String[] locations = { "身份证号", "学号", "邮箱地址" };

	public class LocationDialog extends Dialog {

		public LocationDialog(Context context) {
			super(context, R.style.myDialog);
			// TODO Auto-generated constructor stub
		}

		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.indicatorsdialog);
			DisplayMetrics dm = new DisplayMetrics();
			Window window = getWindow();
			// 取得窗口属性
			window.getWindowManager().getDefaultDisplay().getMetrics(dm);
			// 屏幕的宽度
			int screenWidth = dm.widthPixels;
			WindowManager.LayoutParams wl = window.getAttributes();
			wl.width = screenWidth - dip2px(this.getContext(), 30);
			window.setAttributes(wl);

			ListView listview = (ListView) findViewById(R.id.indicators_list);
			LocationAdapter adapter = new LocationAdapter(
					ChangeEmplInfoActivity.this, "0");
			adapter.setLocation(locations);
			listview.setAdapter(adapter);
			listview.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long i) {
					location_text.setText(locations[position]);
					location_textStr = locations[position];
					switch (position) {
					case 0:
						logintype = "1";
						break;
					case 1:
						logintype = "2";
						break;
					case 2:
						logintype = "3";
						break;
					default:
						break;
					}

					dismiss();
				}
			});
		}

	}

	/**
	 * dip变pix公式
	 * 
	 * @param context
	 * @param dipValue
	 * @return
	 */
	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	private void SendHttpDate(String type, String username, String StredIMs,
			String StredEmail,String StredAddress,
			String StredLinkMan,String StredTel) {
		// String url =
		// "http://120.237.110.50:8097/andriodapp/WebService.asmx?op=EmployeeLogin";
		String url = App.getInstance().url+"/ChangeEmplInfo";
		RequestParams requestparams = new RequestParams(url, "POST");
		requestparams.put("LoginAccount", username);
		requestparams.put("LoginType", logintypes);
		requestparams.put("IMs", StredIMs);
		requestparams.put("Email", StredEmail);
		requestparams.put("Address", StredAddress);
		requestparams.put("LinkMan", StredLinkMan);
		requestparams.put("Tel", StredTel);
		requestparams.put("ReserveField1", "");
		requestparams.put("ReserveField2", "");

		TextHttpResponseHandler textHttpResponseHandler = new TextHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String responseString) {
				// TODO Auto-generated method stub
				Log.e("TAG", "responseString---" + responseString);
				if(responseString!=null&&!"".equals(responseString)){					
					AnalysisData(responseString);
				}

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				// TODO Auto-generated method stub
				Log.e("TAG", "responseString---" + responseString);
//				AnalysisData(responseString);

			}
		};
		try {
			restClient.post(url, requestparams, textHttpResponseHandler);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * json数据解析
	 * 
	 * @param strResult
	 * @throws JSONException
	 */
	private void AnalysisData(String strResult) {
		/**
		 * 这里需要分析服务器回传的json格式数据，
		 */
		JSONObject jsonObject = null;
		try {
			jsonObject = new JSONObject(strResult);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			String returnMsg = jsonObject.getString("returnMsg");
				if (returnMsg == null || "".equals(returnMsg)) {
					Toast.makeText(ChangeEmplInfoActivity.this, "个人资料修改成功",  Toast.LENGTH_SHORT).show();
					Intent intent = new Intent(ChangeEmplInfoActivity.this,
							MainActivity.class);
					startActivity(intent);
					finish();
				} else {
					Toast.makeText(ChangeEmplInfoActivity.this,returnMsg,  Toast.LENGTH_SHORT).show();
				}
			
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
